import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import { defineConfig } from 'vite'
import monkey, { util } from 'vite-plugin-monkey'

import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'

const matchUrl = ['*://*/*']

const excludeUrl = [

]

export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      imports: ['vue', 'vue-router', util.unimportPreset],
      dts: 'types/auto-imports.d.ts',
      resolvers: [
        ElementPlusResolver(),
      ],
    }),
    Components({
      resolvers: [
        ElementPlusResolver(),
      ],
      dts: 'types/components.d.ts',
    }),
    monkey({
      entry: 'src/main.ts',
      userscript: {
        name: '你的篡改猴插件脚本',
        icon: 'https://file.iviewui.com/file/favicon.ico',
        namespace: 'npm/vite-plugin-monkey',
        match: matchUrl,
        exclude: excludeUrl,
        require: ['//gitee.com/xuanxiaoqian_xuan/xuan-monkey_vue3/raw/master/src/cdn/element_plus_index.css'],
        "run-at": 'document-start',
        'connect': '*',
        'noframes': true  // 防止在小窗不断加载引起内存泄漏
      },
      build: {
        externalGlobals: {
          vue: ['Vue', 'https://gitee.com/xuanxiaoqian_xuan/xuan-monkey_vue3/raw/master/src/cdn/vue.global.prod.js', util.dataUrl(';window.Vue=Vue;')],
          axios: ['axios', 'https://gitee.com/xuanxiaoqian_xuan/xuan-monkey_vue3/raw/master/src/cdn/axios.min.js'],
          'element-plus': ['ElementPlus', 'https://gitee.com/xuanxiaoqian_xuan/xuan-monkey_vue3/raw/master/src/cdn/element_plus_index.full.min.js'],
          'sweetalert2': ['Swal', 'https://gitee.com/xuanxiaoqian_xuan/xuan-monkey_vue3/raw/master/src/cdn/sweetalert2@11.js'],
        },
      },
    }),
  ],
  build: {
    // 是否压缩代码
    minify: true
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, './src'),
    },
  },
})
